package com.ikun.admin.modules.sys.iservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikun.admin.modules.sys.entity.*;
import com.ikun.admin.modules.sys.iservice.ILogService;
import com.ikun.admin.modules.sys.service.*;
import com.ikun.admin.modules.sys.vo.*;
import com.ikun.common.result.Result;
import com.ikun.common.search.PageData;
import com.ikun.common.search.PageResult;
import com.ikun.common.search.build.QueryBuilder;
import com.ikun.common.search.build.ResultBuilder;
import com.ikun.common.search.utils.PageSearchUtil;
import com.ikun.common.utils.DataCopy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName IlogServiceImpl
 * @Description:
 * @CreateDate: 2024/5/31 23:52
 */
@Service
public class ILogServiceImpl implements ILogService {

    private static final Logger log = LoggerFactory.getLogger(ILogServiceImpl.class);
    @Autowired
    private LogOperationService logOperationService;

    @Autowired
    private LogErrorService logErrorService;
    @Autowired
    private LogLoginService logLoginService;
    @Autowired
    private LogEmailService logEmailService;
    @Autowired
    private LogOssService logOssService;


    /**
     * 保存操作日志
     * @param logOperation
     */
    @Override
    public void saveOperationLog(LogOperation logOperation) {
        logOperationService.save(logOperation);
    }


    /**
     * 保存异常日志
     * @param logError
     */
    @Override
    public void saveLogError(LogError logError) {
        logErrorService.save(logError);
    }

    @Override
    public Result<PageResult<LogLoginListResultVo>> logLogin(PageData<BaseLogListParamVo> pageData) {
        PageData.PageParam pageParam = pageData.getPageParam();
        BaseLogListParamVo condition = pageData.getCondition();
        Page<LogLogin> logLoginPage = new Page<LogLogin>().setSize(pageParam.getSize()).setCurrent(pageParam.getCurrent());
        QueryWrapper<LogLogin> logLoginQueryWrapper = new QueryWrapper<>();
        logLoginQueryWrapper.lambda().orderByDesc(LogLogin::getCreateTime);
        Boolean build = QueryBuilder.build(logLoginQueryWrapper, condition);
        if (!build) {
            return new Result<PageResult<LogLoginListResultVo>>().success(PageResult.emptyPageResult());

        }
        logLoginService.page(logLoginPage, logLoginQueryWrapper);
        List<LogLoginListResultVo> resultVoList = new ArrayList<>();
        if (logLoginPage.getRecords().size() > 0) {
            ResultBuilder.buildResult(logLoginPage.getRecords(), resultVoList, LogLoginListResultVo.class);
        }
        return new Result<PageResult<LogLoginListResultVo>>().success(PageResult.getInstance(logLoginPage.getTotal(), resultVoList));
    }

    @Override
    public Result<PageResult<LogErrorListResultVo>> logErro(PageData<LogErrorListParamVo> pageData) {
        PageData.PageParam pageParam = pageData.getPageParam();
        BaseLogListParamVo condition = pageData.getCondition();
        Page<LogError> logErrorPage = new Page<LogError>().setSize(pageParam.getSize()).setCurrent(pageParam.getCurrent());
        QueryWrapper<LogError> logErrorQueryWrapper = new QueryWrapper<>();
        logErrorQueryWrapper.lambda().orderByDesc(LogError::getCreateTime);
        Boolean build = QueryBuilder.build(logErrorQueryWrapper, condition);
        if (!build) {
            return new Result<PageResult<LogErrorListResultVo>>().success(PageResult.emptyPageResult());
        }
        logErrorService.page(logErrorPage, logErrorQueryWrapper);
        List<LogErrorListResultVo> resultList = new ArrayList<>();
        if (logErrorPage.getRecords().size() > 0) {
            ResultBuilder.buildResult(logErrorPage.getRecords(), resultList, LogErrorListResultVo.class);
        }
        return new Result<PageResult<LogErrorListResultVo>>().success(PageResult.getInstance(logErrorPage.getTotal(), resultList));
    }

    @Override
    public Result<PageResult<LogEmailListResultVo>> logEmail(PageData<LogEmailListParamVo> pageData) {
        LogEmailListParamVo logEmailListParamVo = pageData.getCondition();
        PageData.PageParam pageParam = pageData.getPageParam();
        Page<LogEmail> page = new Page<LogEmail>().setCurrent(pageParam.getCurrent()).setSize(pageParam.getSize());
        QueryWrapper<LogEmail> logEmailQueryWrapper = new QueryWrapper<>();
        logEmailQueryWrapper.lambda().orderByDesc(LogEmail::getCreateTime);
        Boolean build = QueryBuilder.build(logEmailQueryWrapper, logEmailListParamVo);
        if (!build) {
            return new Result<PageResult<LogEmailListResultVo>>().success(PageResult.emptyPageResult());
        }
        logEmailService.page(page, logEmailQueryWrapper);
        List<LogEmailListResultVo> resultVoList = new ArrayList<>();
        if (page.getRecords().size() > 0) {
            DataCopy.copyToListObj(page.getRecords(), resultVoList, LogEmailListResultVo.class);
        }
        return new Result<PageResult<LogEmailListResultVo>>().success(PageResult.getInstance(page.getTotal(), resultVoList));
    }

    @Override
    public Result<PageResult<LogOssListResultVo>> logOss(PageData<LogOssListParamVo> pageData) {
        LogOssListParamVo listParamVo = pageData.getCondition();
        PageData.PageParam pageParam = pageData.getPageParam();
        Page<LogOss> logOssPage = new Page<LogOss>().setSize(pageParam.getSize()).setCurrent(pageParam.getCurrent());
        QueryWrapper<LogOss> logOssQueryWrapper = new QueryWrapper<>();
        Boolean build = QueryBuilder.build(logOssQueryWrapper, listParamVo);
        if (!build) {
            return new Result<PageResult<LogOssListResultVo>>().success(PageResult.emptyPageResult());
        }
        logOssService.page(logOssPage, logOssQueryWrapper);
        List<LogOssListResultVo> resultVoList = new ArrayList<>();
        if (logOssPage.getRecords().size() == 0) {
            ResultBuilder.buildResult(logOssPage.getRecords(), resultVoList, LogOssListResultVo.class);
        }
        return new Result<PageResult<LogOssListResultVo>>().success(PageResult.getInstance(logOssPage.getTotal(), resultVoList));
    }

    @Override
    public Result<PageResult<LogOperationListResultVo>> logOperation(PageData<LogOperationListParamVo> pageData) {
        PageResult<LogOperationListResultVo> logOperationListResultVoPageResult = PageSearchUtil.buildPageSearch(pageData, logOperationService, LogOperationListResultVo.class);
        return new Result<PageResult<LogOperationListResultVo>>().success(logOperationListResultVoPageResult);
    }
}
